package org.cwnuit.course.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.cwnuit.course.dao.ClassroomDAO;
import org.cwnuit.course.domain.Classroom;
import org.cwnuit.course.domain.Coursebox;

/**
 * 教室信息表Service
 * @author john
 */
public class ClassroomService {
	private ClassroomDAO classroomDAO =  new ClassroomDAO();
	/**
	 * 通过教室ID获取教室信息
	 * @param objectid
	 */
	public Classroom getClassroom(Integer roomid){
		return classroomDAO.getClassroom(roomid);
	}
	
	/**
	 * 获取用所有教室信息
	 * @return
	 */
	public List<Classroom> listClassroom(){
		return classroomDAO.listClassroom();
	}
	
	/**
	 * 添加或修改教室信息
	 * @param request
	 */
	public boolean saveClassroom(HttpServletRequest request) {
		String roomidStr = request.getParameter("roomid")==""?"0":request.getParameter("roomid");
		int roomid = Integer.parseInt(roomidStr);
		Coursebox coursebox = (Coursebox)request.getSession().getAttribute("coursebox");
		int courseid = coursebox.getCourseid();
		String roomname = request.getParameter("roomname");
		int maxnum = Integer.parseInt(request.getParameter("maxnum"));
		String remarks = request.getParameter("remarks");
		
		Classroom classroom = new Classroom();
		classroom.setCourseid(courseid);
		classroom.setRoomid(roomid);
		classroom.setRoomname(roomname);
		classroom.setMaxnum(maxnum);
		classroom.setRemarks(remarks);
		try{
			if(roomid != 0){
				classroomDAO.updateClassroom(classroom);
			}else{
				classroomDAO.insertClassroom(classroom);
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 删除教室信息
	 * @param userid
	 */
	public boolean deleteClassroom(Integer roomid){
		try{
			Classroom classroom = new Classroom();
			classroom.setRoomid(roomid);
			classroomDAO.deleteClassroom(classroom);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
	}
}
